import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import jwtDecode from 'jwt-decode'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  }
  ,
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: '/users',
        name: 'users',
        meta: {
          title: '用户管理'
        },
        component: () => import('../views/Users.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        meta: {
          title: '角色列表'
        },
        component: () => import('../views/Roles.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        meta: {
          title: '权限管理'
        },
        component: () => import('../views/Rights.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        meta: {
          title: '商品列表'
        },
        component: () => import('../views/Goods.vue')
      },
      {
        path: '/params',
        name: 'params',
        meta: {
          title: '分类参数'
        },
        component: () => import('../views/Params.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        meta: {
          title: '商品分类'
        },
        component: () => import('../views/Categories.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        meta: {
          title: '权限列表'
        },
        component: () => import('../views/Orders.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        meta: {
          title: '数据报表'
        },
        component: () => import('../views/Reports.vue')
      },
    ]
  }
]

const router = new VueRouter({
  routes
})
//守卫
router.beforeEach((to, from, next) => {
  
  if (to.path == '/login') next();
  // console.log(to.path)
  //查看token
  let token = localStorage.getItem('token');
  // console.log(token,"222")
  let result = jwtDecode(token)
  // console.log(result,"111")
  let nowtime = parseInt(new Date().getTime() / 1000)
  if (token) {
    if (nowtime - result.exp > 0) {
      next('/login')
    } else {
      next()
    }
  } else {
    next('/login')
  }
})
export default router
